/**
 * Copyright 2012 Tom Hombergs (tom.hombergs@gmail.com)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.wicketcharts.highcharts;

import java.text.MessageFormat;

import javax.swing.text.html.Option;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.html.IHeaderResponse;

import com.googlecode.wicketcharts.JavaScriptResourceRegistry;
import com.googlecode.wicketcharts.highcharts.json.JsonRenderer;
import com.googlecode.wicketcharts.highcharts.options.ChartOptions;
import com.googlecode.wicketcharts.highcharts.options.Options;

/**
 * This behavior takes in an {@link Options} object containing the configuration
 * of a chart and calls the proper javascript to display the chart in the
 * component to which this behavior is added.
 * 
 * @author Tom Hombergs (tom.hombergs@gmail.com)
 * 
 */
public class ChartBehavior extends Behavior {

	private static final long serialVersionUID = 1L;

	private final Chart container;

	/**
	 * Constructor.
	 * 
	 * @param options
	 *          the options for the chart. The {@link Option} class is very
	 *          similar in structure to the Highcharts API reference, see
	 *          http://www.highcharts.com/ref/.
	 */
	public ChartBehavior(final Chart container) {
		this.container = container;
	}

	private boolean needsExporting(final Options options) {
		return options.getExporting() == null
		    || (options.getExporting().getEnabled() != null && options.getExporting().getEnabled());
	}

	private boolean needsMore(final Options options) {
		return options.getChartOptions() != null && options.getChartOptions().getPolar() != null
		    && options.getChartOptions().getPolar();
	}

	@Override
	public void renderHead(final Component component, final IHeaderResponse response) {
		Options options = this.container.getOptions();

		// we need an ID to connect this component with Highcharts
		component.setOutputMarkupId(true);

		// include javascript dependencies
		JavaScriptResourceRegistry.getInstance().getJQueryEntry().addToHeaderResponse(response);
		JavaScriptResourceRegistry.getInstance().getHighchartsEntry().addToHeaderResponse(response);
		if (needsExporting(options)) {
			JavaScriptResourceRegistry.getInstance().getHighchartsExportingEntry().addToHeaderResponse(response);
		}
		if (needsMore(options)) {
			JavaScriptResourceRegistry.getInstance().getHighchartsMoreEntry().addToHeaderResponse(response);
		}

		// define markup id
		final String id = component.getMarkupId();
		if (options.getChartOptions() == null) {
			options.setChartOptions(new ChartOptions());
		}
		options.getChartOptions().setRenderTo(id);

		JsonRenderer renderer = new JsonRendererFactory().createJsonRenderer();

		// set theme
		if (this.container.getTheme() != null) {
			response.renderOnDomReadyJavaScript(MessageFormat.format("Highcharts.setOptions({0});",
			    renderer.toJson(this.container.getTheme())));
		} else if (this.container.getThemeUrl() != null) {
			response.renderJavaScriptReference(this.container.getThemeUrl());
		} else if (this.container.getThemeReference() != null) {
			response.renderJavaScriptReference(this.container.getThemeReference());
		}

		// define javascript variable
		String chartVarname = "highchart_" + id;
		response
		    .renderJavaScript(MessageFormat.format("var {0};", chartVarname), "highcharts-" + chartVarname + "-declare");
		response.renderOnDomReadyJavaScript(MessageFormat.format("{0} = new Highcharts.Chart({1});", chartVarname,
		    renderer.toJson(options)));
	}

}
